package vn.co.thn.web.hotvideo.api;

import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import vn.co.thn.web.hotvideo.common.Helper;
import vn.co.thn.web.hotvideo.data.jpa.UploadHistory;
import vn.co.thn.web.hotvideo.data.jpa.Video;
import vn.co.thn.web.hotvideo.json_object.Json_Upload;
import vn.co.thn.web.hotvideo.model.UploadHistoryModel;
import vn.co.thn.web.hotvideo.model.VideoModel;

import com.google.appengine.api.datastore.Text;
import com.google.gson.Gson;
@Path("/upload_history")
public class ApiUploadHistory {
	@Path("/add")
	@POST
	@Produces("application/json")
	public String AddHistory(@FormParam("DeviceId") String DeviceId, @FormParam("PathFile") String PathFile, @FormParam("FileSize") String FileSize,
			@FormParam("embed_code") String embed_code, @FormParam("title") String Title, @FormParam("content") String Content,
			@FormParam("category_key") String Category_key, @FormParam("email") String Email) {
		Json_Upload json_Upload = new Json_Upload();
		UploadHistory uploadHistory=null;
		UploadHistoryModel uploadHistoryModel=new UploadHistoryModel();
		uploadHistory=uploadHistoryModel.getObject((DeviceId + PathFile + FileSize+Email).trim());
		if (uploadHistory == null) {
			uploadHistory=new UploadHistory();
			uploadHistory.setDeviceId(DeviceId);
			uploadHistory.setPathFile(PathFile);
			uploadHistory.setFileSize(FileSize);
			uploadHistory.setEmail(Email);
			uploadHistory.setKey((DeviceId + PathFile + FileSize+Email).trim());
			uploadHistoryModel.Add(uploadHistory);
			VideoModel videoModel=new VideoModel();
			Video video = new Video();
			String date_post=Helper.CurrentDateTime();
			video.setEmbed_code(embed_code);
			video.setCategory_Key(Category_key);
			video.setTitle(Title);
			video.setDeviceId(DeviceId);
			video.setEmbed_code(embed_code);
			video.setContent(new Text(Content));
			video.setDate_Update(date_post);
			video.setImage_Url("http://img.youtube.com/vi/" + video.getEmbed_code() + "/0.jpg");
			video.setContent_Search( Title+ " "+Content);
			videoModel.Add(video);
			json_Upload.setError_code(0);
			json_Upload.setUploadHistory(uploadHistory);
		}else{
			json_Upload.setError_code(1);
			json_Upload.setUploadHistory(uploadHistory);
		}
		return new Gson().toJson(json_Upload);

	}
}
